
#include "dict.h"
#include "http_request.h"
#include "zmalloc.h"
#include "mylog.h"

#include <string.h>
#include <stdio.h>

static int requestId = 0;
struct HttpRequest* initHttpRequest(enum RequestMethod requestMethod) {
	struct HttpRequest* httpRequest = (struct HttpRequest* ) zcalloc( sizeof(struct HttpRequest) );
	if (!httpRequest) {
		return NULL;
	}

	httpRequest->requestMethod = requestMethod;
	httpRequest->id = requestId;
	mylog(LOG_INFO, "init HttpRequest[%d]", httpRequest->id);
	return httpRequest;
}

void destoryHttpRequest(struct HttpRequest* httpRequest) {
	if (!httpRequest) {
		mylog(LOG_INFO, "DESTROY http request!!!");
		return;
	}
	mylog(LOG_INFO, "destroy HttpRequest[%d]", httpRequest->id);
	zfree(httpRequest);
	return;
}

#define	REQUESTMETHOD_BUFLEN	1024
int setRequestMethod(struct HttpRequest* httpRequest, const char* buf, int bufLen) {
	if (!httpRequest) {
		return -1;
	}

	if (bufLen > REQUESTMETHOD_BUFLEN) {
		return -2;
	}

	char method[REQUESTMETHOD_BUFLEN];
	memcpy(method, buf, bufLen);
	method[bufLen] = 0;
	mylog(LOG_INFO, "method: %s", method);
	if (strcasecmp(method, "GET") == 0) {
		httpRequest->requestMethod = GetRequest;
		return 0;
	} else {
		httpRequest->requestMethod = UnKnownRequest ;
		return -3;
	}
}

int setRequestPath(struct HttpRequest* httpRequest, const char* buf, int bufLen) {
	if (!httpRequest) {
		return -1;
	}

	if (bufLen > DEAFAULT_REQUESTPATH_LENGTH) {
		return -2;
	}
	mylog(LOG_INFO, "path: %s", buf);
	memcpy(httpRequest->requestPath, buf, bufLen);
	httpRequest->requestPath[bufLen] = 0;

	return 0;
}

